package com.carboy.launch.widgets;

import android.app.Dialog;
import android.content.Context;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.carboy.launch.R;

/**
 * 加载Dialog
 */
public class LoadingDisplayDialog extends Dialog {

	private TextView ldd_title_tv;

	public LoadingDisplayDialog(Context context) {
		super(context, R.style.Theme_Light_FullScreenDialogOperate);
		// 绑定Layout
		this.setContentView(R.layout.dialog_loading_display);
		// 初始化控件
		ldd_title_tv = (TextView) this.findViewById(R.id.ldd_title_tv);
		// 禁止返回键关闭(模拟可以返回键关闭Dialog，然后立即点击其他页面，看是否会报错----请求数据异步操作，赋值的时候报null(页面已Stop或者销毁了)----测试)
		// this.setCancelable(false);
	}

	/**
	 * 设置透明度(针对Dialog)
	 */
	public void setAlpha(float alpha) {
		Window window = LoadingDisplayDialog.this.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = alpha;
		window.setAttributes(lp);
	}

	/**
	 * 设置背景透明度(针对Dialog背景)
	 */
	public void setDimAmount(float dimAmount) {
		Window window = LoadingDisplayDialog.this.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.dimAmount = dimAmount;
		window.setAttributes(lp);
		// 背景模糊
		// window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}

	public void showDialog() {
		this.showDialog(null);
	}

	public void showDialog(String title) {
		if (title == null) {
			ldd_title_tv.setText("加载中,请稍后...");
		} else {
			ldd_title_tv.setText(title);
		}
		LoadingDisplayDialog.this.show();
	}

	/**
	 * 关闭Dialog
	 */
	public void cancelDialog() {
		if (this.isShowing()) {
			this.cancel();
		}
	}
}
